package com.example.alibabanacosdiscoveryclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaNacosDiscoveryClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(AlibabaNacosDiscoveryClientApplication.class, args);
    }

    @RestController
    static class TestController{
        @Autowired
        LoadBalancerClient loadBalancerClient;

        @GetMapping("/test")
        public String test(){
            ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
            // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
            //使用了Spring Cloud Common中的 LoadBalancerClient 接口来挑选服务实例信息
            String url = serviceInstance.getUri() + "/hello?name=" + "呵呵";
            RestTemplate restTempalte = new RestTemplate();
            String result = restTempalte.getForObject(url, String.class);
            return "Invoke:" + url + ",return:" + result;
        }
    }

}
